package com.yaoweigroup.community.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yaoweigroup.community.R;
import com.yaoweigroup.community.enity.BBSContent;

public class BBSAdapter extends BaseAdapter {

	private Context context;
	private List<BBSContent> list;
	private LayoutInflater mInflater;

	public BBSAdapter(Context context, List<BBSContent> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_bbs_item, null);
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.click_tv = (TextView) convertView
					.findViewById(R.id.click_tv);
			holder.content_tv = (TextView) convertView
					.findViewById(R.id.content_tv);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.replay_tv = (TextView) convertView
					.findViewById(R.id.replay_tv);

		} else {
			holder = (Holder) convertView.getTag();
		}

		switch (position % 2) {
		case 0:
			holder.icon_iv.setImageResource(R.drawable.llq_num_1);
			break;
		case 1:
			holder.icon_iv.setImageResource(R.drawable.llq_num_2);

			break;
		case 2:
			holder.icon_iv.setImageResource(R.drawable.llq_num_3);

			break;
		default:
			holder.icon_iv.setImageResource(R.drawable.llq_num_1);
			break;
		}
		BBSContent content = list.get(position);
		holder.name_tv.setText(content.getAuthor());

		// String contentStr = content.getContent();
		// if (contentStr.length() > 10) {
		// holder.content_tv.setText(Html.fromHtml(content.getContent()
		// .substring(0, 10)) + "......");
		// } else {
		// holder.content_tv.setText(Html.fromHtml(content.getContent()));
		// }

		holder.content_tv.setText(content.getPosttitle());

		holder.time_tv.setText(content.getCretetime());
		holder.replay_tv.setVisibility(View.GONE);
		holder.replay_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		holder.click_tv.setText("点击数 " + content.getReadcount());
		return convertView;
	}

	public final class Holder {

		TextView name_tv;
		ImageView icon_iv;
		TextView click_tv;
		TextView content_tv;
		TextView time_tv;
		TextView replay_tv;
	}

	public void setDataChange(List<BBSContent> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
